package com.cheng.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheng.admin.constant.Constants;
import com.cheng.admin.dao.PostPictureOrderMapper;
import com.cheng.admin.model.Order;
import com.cheng.admin.model.PostPictureOrder;
import com.cheng.admin.model.Template;
import com.cheng.admin.model.UnitPrice;
import com.cheng.admin.service.PostPictureOrderService;
import com.cheng.admin.service.TemplateService;
import com.cheng.admin.service.UnitPriceService;
import com.cheng.common.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 寄信订单 服务实现类
 * </p>
 *
 * @author 起凡
 * @since 2022-01-17
 */
@Slf4j
@Service(value = Constants.ORDER_TYPE_LETTER)
public class PostPictureOrderServiceImpl extends ServiceImpl<PostPictureOrderMapper, PostPictureOrder> implements PostPictureOrderService {
    @Autowired
    UnitPriceService unitPriceService;
    @Autowired
    TemplateService templateService;
    @Autowired
    OrderProductServiceImpl orderProductService;

    /**
     * 计算明普通信件的价格
     *
     * @param order
     */
    @Override
    public void preprocess(Order order) {
        PostPictureOrder postPictureOrder = order.getPostPictureOrder();
        // 照片价格计算
        BigDecimal picturePrice = BigDecimal.ZERO;
        if (postPictureOrder.getPictureUnitId() != null) {
            UnitPrice unitPicture = unitPriceService.getById(postPictureOrder.getPictureUnitId());
            // 超出起步件的件数
            int pictureLength = JSON.parseArray(postPictureOrder.getPictureUrls()).size();
            log.info("" + pictureLength + " " + postPictureOrder.getPictureUrls());
            int extraPicture = pictureLength - unitPicture.getFirst().intValue();
            if (extraPicture > 0) {
                if (unitPicture.getRenewalPrice().compareTo(BigDecimal.ZERO) <= 0) {
                    throw new CustomException("照片超过上限");
                }
                //总价=起步价格+(超出件/续价单位)*续件价格
                picturePrice = unitPicture.getFirstPrice()
                        .add(BigDecimal.valueOf(extraPicture)
                                .divide(unitPicture.getRenewal(), 2)
                                .multiply(unitPicture.getRenewalPrice()));
            } else {
                picturePrice = unitPicture.getFirstPrice();
            }

        }
        // 字数价格计算
        BigDecimal wordPrice = BigDecimal.ZERO;
        if (postPictureOrder.getWordUnitId() != null) {
            UnitPrice unitWord = unitPriceService.getById(postPictureOrder.getWordUnitId());
            int extraWord = postPictureOrder.getWordContent().length() - unitWord.getFirst().intValue();
            if (extraWord > 0) {
                if (unitWord.getRenewalPrice().compareTo(BigDecimal.ZERO) <= 0) {
                    throw new CustomException("字数超过上限");
                }
                wordPrice = unitWord.getFirstPrice()
                        .add(BigDecimal.valueOf(extraWord)
                                .divide(unitWord.getRenewal(), 2)
                                .multiply(unitWord.getRenewalPrice()));

            } else {
                wordPrice = unitWord.getFirstPrice();
            }
        }
        //手写信费用
        BigDecimal writePrice = BigDecimal.ZERO;
        if (postPictureOrder.getWriteUnitId() != null) {
            UnitPrice unitWrite = unitPriceService.getById(postPictureOrder.getWriteUnitId());
            // 超出起步件的件数
            int writeLength = JSON.parseArray(postPictureOrder.getWritePictureUrls()).size();
            int extraWrite = writeLength - unitWrite.getFirst().intValue();
            if (extraWrite > 0) {
                if (unitWrite.getRenewalPrice().compareTo(BigDecimal.ZERO) <= 0) {
                    throw new CustomException("手写照片超过上限");
                }
                //总价=起步价格+(超出件/续价单位)*续件价格
                writePrice = unitWrite.getFirstPrice()
                        .add(BigDecimal.valueOf(extraWrite)
                                .divide(unitWrite.getRenewal(), 2)
                                .multiply(unitWrite.getRenewalPrice()));
            } else {
                writePrice = unitWrite.getFirstPrice();
            }
        }
        // 模板费用计算
        Template template = templateService.getById(postPictureOrder.getTemplateId());
        if (template == null) {
            throw new CustomException("模板不能为空");
        }
        postPictureOrder.setTemplate(template);
        BigDecimal templatePrice = template.getPrice();
        //计算商品价格
        if (postPictureOrder.getOrderProducts() != null && postPictureOrder.getOrderProducts().size() > 0) {
            order.setOrderProducts(postPictureOrder.getOrderProducts());
            // 商品价格加在原价上
            orderProductService.preprocess(order);
        }
        // 计算出价格
        postPictureOrder.setPicturePrice(picturePrice).setWordPrice(wordPrice).setWritePicturePrice(writePrice)
                .setTemplatePrice(templatePrice);
        postPictureOrder.setOrderId(order.getId());
        //在原价上加上所有价格(商品价格在处理的时候就已经加上去了)
        order.setOriginPrice(order.getOriginPrice().add(picturePrice).add(wordPrice).add(writePrice).add(templatePrice));
        order.setPriceDetail(JSON.toJSONString(postPictureOrder));

        save(postPictureOrder);
    }

    @Override
    public void rollbackPreprocess(Order order) {
        PostPictureOrder postPictureOrder = JSON.parseObject(order.getPriceDetail(), PostPictureOrder.class);
        if (postPictureOrder.getOrderProducts() != null && postPictureOrder.getOrderProducts().size() > 0) {
            orderProductService.rollbackPreprocess(order);
        }
    }
}
